package com.virtualightning.mvvm.base

import android.content.Intent
import android.content.pm.PackageManager
import android.content.res.AssetManager
import android.content.res.Resources
import android.os.Bundle
import android.util.Log
import android.view.MotionEvent
import android.view.View
import android.view.Window
import android.view.WindowManager
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.Lifecycle
import com.virtualightning.mvvm.impl.proxy.HostActivity
import com.virtualightning.mvvm.impl.proxy.IProxyInterface

/**
 *  Anchor : Create by CimZzz
 *  Time : 2021/1/27 17:04:01
 *  Project : zxs-new
 *  寄生 UI 基类
 */
abstract class BaseSpawnUI : BaseUI(), IProxyInterface {
    private var hostActivity: HostActivity? = null

    ///////////////////////////////////////////////////////////////////////////
    // 代码分割线 on 2021/1/27 下午5:08 by CimZzz
    // 以下内容为:
    // 代理宿主方法
    ///////////////////////////////////////////////////////////////////////////

    override fun setContentView(view: View?) {
        hostActivity?.setContentView(view) ?: super.setContentView(view)
    }

    override fun setContentView(layoutResID: Int) {
        hostActivity?.setContentView(layoutResID) ?: super.setContentView(layoutResID)
    }

    override fun <T : View> findViewById(id: Int): T? {
        return hostActivity?.findViewById<T>(id) ?: super.findViewById(id)
    }

    override fun getTaskId(): Int {
        return hostActivity?.taskId ?: super.getTaskId()
    }

    override fun getWindow(): Window {
        return hostActivity?.window ?: super.getWindow()
    }

    override fun getWindowManager(): WindowManager {
        return hostActivity?.windowManager ?: super.getWindowManager()
    }

    override fun getTheme(): Resources.Theme {
        return hostActivity?.theme ?: super.getTheme()
    }

    override fun getPackageManager(): PackageManager {
        return hostActivity?.packageManager ?: super.getPackageManager()
    }

    override fun getSupportFragmentManager(): FragmentManager {
        return hostActivity?.supportFragmentManager ?: super.getSupportFragmentManager()
    }

    override fun getLifecycle(): Lifecycle {
        return hostActivity?.lifecycle ?: super.getLifecycle()
    }

    override fun getClassLoader(): ClassLoader {
        return hostActivity?.classLoader ?: super.getClassLoader()
    }

    override fun getResources(): Resources {
        return hostActivity?.resources ?: super.getResources()
    }

    override fun getAssets(): AssetManager {
        return hostActivity?.assets ?: super.getAssets()
    }

    ///////////////////////////////////////////////////////////////////////////
    // 代码分割线 on 2021/1/27 下午5:08 by CimZzz
    // 以下内容为:
    // 寄生 Activity 实现的方法
    ///////////////////////////////////////////////////////////////////////////

    override fun attach(activity: HostActivity) {
        this.hostActivity = activity
        attachBaseContext(activity.baseContext)
    }

    override fun onRealCreated(savedInstanceState: Bundle?) {

    }

    override fun onStart() {
        if(hostActivity == null) {
            super.onStart()
        }
    }

    override fun onResume() {
        if(hostActivity == null) {
            super.onResume()
        }
    }

    override fun onPause() {
        if(hostActivity == null) {
            super.onPause()
        }
    }

    override fun onStop() {
        if(hostActivity == null) {
            super.onStop()
        }
    }

    override fun onDestroy() {
        if(hostActivity == null) {
            super.onDestroy()
        }
    }

    override fun onSaveInstanceState(outState: Bundle) {
        if(hostActivity == null) {
            super.onSaveInstanceState(outState)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if(hostActivity == null) {
            super.onActivityResult(requestCode, resultCode, data)
        }
    }
}